import request from '@/utils/request'

// pinia滚动加载
export const useList = defineStore({
  id: 'list',
  state: () => {
    return {
      newList: [],
      page: 1,
    }
  },
  actions: {
    // pinia 请求
    getListRequest($state: any) {
      request({
        url: '/comments?_limit=20&_page=' + this.page,
      })
        .then((res: any) => {
          if (res.length < 20) {
            $state.complete()
          } else {
            this.newList.push(...(res as []))
            $state.loaded()
          }
          this.page++
        })
        .catch(() => {
          $state.error()
        })
    },
  },
})
